﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RBS.Web.API.Models;
using JsonData = RBS.Common.JsonEntity.JsonData;

namespace RBS.Web.API.Areas.Adv.Controllers
{
    public class SelectAdvController : ApiController
    {
        private readonly ApiContext _db = new ApiContext(); 
        // GET api/selectadv/5
        public JsonData Get(int id)
        {
            var jsonData = new JsonData {Success = true};
            var advList = new RBS.Core.BLL.Sys_Adv().GetModelList(" AdvStatus=1");
            var advUrl = RBS.Common.ConfigHelper.GetConfigString("advUrl");
            List<object> items = new List<object>(); 
            foreach (var adv in advList)
            {
                items.Add(
                    new
                        {
                            Id=adv.Id,
                            AdvName=adv.AdvName,
                            AdvDesc=adv.AdvDesc,
                            AdvLink=adv.AdvLink,
                            AdvImg = advUrl+adv.AdvImg.Replace("\\","/")
                        }
                    );
            }

            jsonData.Data = items;
            return jsonData;
        }

        /// <summary>
        /// 获取菜谱图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /*private byte[] GetadvImg(int id)
        {
          /*  var jsonData = new JsonData();
            var imgpach = RBS.Common.ConfigHelper.GetConfigString("imgpach");
            var hHotelImg = _db.adv.Where(i => i.Id == id).OrderBy(i => i.Id).FirstOrDefault();
            if (hHotelImg != null)
            {
                string imgurl = imgpach +
                                hHotelImg.DishImages;
                jsonData.Success = true;
                return GetImageByte(imgurl);
            }#1#
            return null;
        }*/
        /// <summary>
        /// 根据图片路径返回字节流
        /// </summary>
        /// <param name="imagePath"></param>
        /// <returns></returns>
        private byte[] GetImageByte(string imagePath)
        {
            FileStream files = new FileStream(imagePath, FileMode.Open);
            byte[] imgByte = new byte[files.Length];
            files.Read(imgByte, 0, imgByte.Length);
            files.Close();
            return imgByte;
        }
    }
}
